//
//  SingPickViewController.swift
//  hunan_ios_ldnydfuz
//
//  Created by yzd on 2019/5/9.
//  Copyright © 2019 ldnsm. All rights reserved.
//




import UIKit

protocol singDelegate {
    func didSet(index:Int,value:String)
}

class SingPickViewController: UIViewController {
    
    var delegate: singDelegate?
    var pickerView:UIPickerView!
    var originData : [String] = []
    var index:Int = 0
    var value :String = ""
    fileprivate var overVeiw = UIView()
    
    required init() {
        super.init(nibName: nil, bundle: nil)
        view.backgroundColor = UIColor.clear
        self.providesPresentationContextTransitionStyle = true
        self.definesPresentationContext = true
        self.modalPresentationStyle = .custom
        
        // 初始化UI
        //        setupUIViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        UIView.animate(withDuration: 0.25) {
            self.overVeiw.alpha = 0.5
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //初始化数据
       
    }
}

// MARK: 初始化UI
extension SingPickViewController: UIPickerViewDelegate, UIPickerViewDataSource {
    
    
    func setupUIViews() {
        
        overVeiw = UIView()
        overVeiw.backgroundColor = UIColor.black
        overVeiw.alpha = 0.0
        view.addSubview(overVeiw)
        
        overVeiw.snp.makeConstraints {
            $0.width.equalTo(screenWidth)
            $0.height.equalTo(screenHeight)
        }
        
        
        
        //创建选择器
        pickerView=UIPickerView()
        pickerView.backgroundColor = UIColor.white
        //将dataSource设置成自己
        pickerView.dataSource=self
        //将delegate设置成自己
        pickerView.delegate=self
        view.addSubview(pickerView)
        pickerView.snp.makeConstraints {
            $0.width.equalTo(screenWidth)
            $0.height.equalTo(250)
            $0.bottom.equalToSuperview()
        }
        
        let topView = UIView()
        topView.backgroundColor = UIColor.white
        view.addSubview(topView)
        topView.snp.makeConstraints {
            $0.width.equalToSuperview()
            $0.height.equalTo(40)
            $0.bottom.equalTo(pickerView.snp.top)
        }
        
        //建立一个按钮，触摸按钮时获得选择框被选择的索引
        let button = UIButton()
        button.setTitle("确定",for:.normal)
        button.addTarget(self, action:#selector(self.getPickerViewValue), for: .touchUpInside)
        button.setTitleColor(UIColor.black, for: .normal)
        topView.addSubview(button)
        button.snp.makeConstraints {
            $0.width.equalTo(100)
            $0.height.equalTo(30)
            $0.top.equalToSuperview().offset(10)
            $0.right.equalToSuperview().offset(-10)
        }
    }
    

    

    
    //设置选择框各选项的内容，继承于UIPickerViewDelegate协议
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int,
                    forComponent component: Int) -> String? {
        return self.originData[row]
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView,
                    numberOfRowsInComponent component: Int) -> Int {
        return self.originData.count
    }
    //选中项改变事件（将在滑动停止后触发）
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,
                    inComponent component: Int) {
        //根据列、行索引判断需要改变数据的区域
        index = row
       
       
    }
    
    //触摸按钮时，获得被选中的索引
    @objc func getPickerViewValue(){
        //获取选中的省
         value = self.originData[index]
         self.dismiss(animated: false, completion: nil)
        self.delegate?.didSet(index: index, value: value)
        
    }
}

// MARK: 点击屏幕弹出退出
extension SingPickViewController {
    
    func sheetViewDismiss() {
        UIView.animate(withDuration: 0.25, animations: {
            self.overVeiw.alpha = 0
        }) { (_) in
            self.dismiss(animated: false, completion: nil)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        sheetViewDismiss()
    }
    
    @objc func cancelBtnDidClick(btn: UIButton) {
        sheetViewDismiss()
    }
}
